package com.lagou.controller;

import com.lagou.UserFegin;
import com.lagou.dao.UserMapper;
import com.lagou.dao.entity.User;
import com.lagou.dao.entity.UserQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Random;
import java.util.UUID;

/**
 * @ClassName UserContrller
 * @Description
 * @Author cxz
 * @Date 2020/7/8 15:13
 * Version V1.0
 */
@RestController
@RequestMapping("/user")
public class UserContrller {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserFegin userFegin;

    @RequestMapping("/isRegistered/{email}")
    public Boolean getUser(@PathVariable("email") String email) {
        User user = userMapper.getUser(email);
        if (null == user) {
            return false;
        }
        return true;
    }

    @RequestMapping("/login/{email}/{password}")
    public String checkUser(@PathVariable("email") String email, @PathVariable("password") String password, HttpServletResponse httpServletResponse) {
        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        User users = userMapper.getUser(user.getEmail());
        if (null == users) {
            return "用户不存在";
        }
        if (!users.getPassword().equalsIgnoreCase(user.getPassword())) {
            return "密码错误";
        }
        String username = user.getEmail();
        String passwords = user.getPassword();
        String a = username + passwords;
        String token = UUID.nameUUIDFromBytes(a.getBytes()).toString();
        userMapper.saveToken(token, user.getEmail());
        Cookie cookie = new Cookie("token", token);
        httpServletResponse.addCookie(cookie);
        return "true";

    }

    @RequestMapping("/register/{email}/{password}/{code}")
    public Object addUser(@PathVariable("email") String email, @PathVariable("password") String password
            , @PathVariable("code") String code, HttpServletResponse httpServletResponse) {
        UserQuery user = new UserQuery();
        user.setCode(code);
        user.setEmail(email);
        user.setPassword(password);
        User users = userMapper.getUser(user.getEmail());
        if (null == users) {
            String codeByEmail = userMapper.getCodeByEmail(user.getEmail());
            if (StringUtils.isEmpty(codeByEmail)) {
                return "验证码超时";
            }
            if (!codeByEmail.equals(user.getCode())) {
                return "验证码错误";
            }
            User user1 = new User();
            user1.setPassword(user.getPassword());
            user1.setEmail(user.getEmail());
            userMapper.insertUser(user1);
            String username = user.getEmail();
            String passwords = user.getPassword();
            String a = username + passwords;
            String token = UUID.nameUUIDFromBytes(a.getBytes()).toString();
            Cookie cookie = new Cookie("token", token);
            httpServletResponse.addCookie(cookie);
            return "1";
        } else {
            return "用户已存在";
        }
    }

    @RequestMapping("/info/{token}")
    public String getEmail(@PathVariable("token") String token) {
        String emailBytoken = userMapper.getEmailBytoken(token);
        return emailBytoken;
    }

    /**
     * 功能描述
     * 发送邮件
     */
    @RequestMapping("/sendEmail")
    public Object sendEmail(String email) {
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < 6; i++) {
            Integer random = getRandom(0, 9);
            stringBuilder.append(random);
        }
        //  String o = userFegin.sendEmail(email, Integer.valueOf(stringBuilder.toString()));
        userMapper.saveCode(email, stringBuilder.toString());

        return "";
    }

    public Integer getRandom(int start, int end) {
        int max = 0;
        int min = 0;
        if (start >= end) {
            max = start;
            min = end;
        } else {
            max = end;
            min = start;
        }
        Random random = new Random();
        int s = random.nextInt(max) % (max - min + 1) + min;
        return s;
    }
}
